<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf8"/>
    <link rel="shortcut icon" href="#">
  </head>
  <body>
    <script id="testScript"></script>
    <script src="./post_data.js"></script>
    <script>
     const testUrl = async ({url, sessionId, readCookies, writeCookies, type}) => {
       let passed;
       const isScript = url.includes(".js") || type === "script";
       const testElement = document.createElement(isScript ? "script" : "img");
       document.body.appendChild(testElement);
       const url2 = new URL(url);
       url2.searchParams.append("sessionId", sessionId);
       if (readCookies) {
         url2.searchParams.append("pto_read_cookie", "true");
       }
       if (writeCookies) {
         url2.searchParams.append("pto_write_cookie", "true");
       }
       const resultPromise = new Promise((resolve, reject) => {
         testElement.addEventListener("load", e => resolve(e), { once: true});
         testElement.addEventListener("error", e => reject(e), { once: true});
       });
       testElement.src = url2.href;
       try {
         let result = await resultPromise;
         // DuckDuckGo and Brave inject non-tracking surrogate scripts, so should pass test even though the load succeeds.
         // https://github.com/duckduckgo/tracker-surrogates/blob/main/surrogates/chartbeat.js
         // https://github.com/brave/uBlock/blob/master/src/web_accessible_resources/chartbeat.js
         if (window.pSUPERFLY &&
             (window.pSUPERFLY.__proto__.toString() === '() => {}' ||
              ((window.pSUPERFLY.activity.toString() === 'function() {\n    }') &&
               (window.pSUPERFLY.virtualPage.toString() === 'function() {\n    }')))) {
           passed = true;
           console.log(url, "surrogate script detected");
         } else {;
           passed = false;
           console.log(url, result);
         }
       } catch (err) {
         passed = true;
         console.log(url, err);
       }
       return passed;
     };

     const runTest = async () => {
       const searchParams = new URLSearchParams(window.location.search);
       const sessionId = searchParams.get("sessionId");
       const readCookies = searchParams.get("read_cookies") === "true";
       const writeCookies = searchParams.get("write_cookies") === "true";
       const response = await fetch("./trackers.json", { headers: {'Accept': 'application/json' } });
       const tests = await response.json();
       const results = Object.fromEntries(await Promise.all(tests.map(async ({name, url, type}) => {
         const passed = await testUrl({url, sessionId, readCookies, writeCookies, type});
         const description = `Tests whether the browser blocks the page from loading the tracker at ${url}`;
         return [name, { url, passed, description }];
       })));
       console.log(results);
       await postDataAndCarryOn(results, (readCookies || writeCookies) ? "tracking_cookies" : "trackers");
     };
     runTest();
    </script>
  </body>
</html>
